package minuy.android.slowcarremotecontrol.service;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.SeekBar;
import android.widget.Toast;

import minuy.android.slowcarremotecontrol.MainActivity;
import minuy.android.slowcarremotecontrol.R;
import minuy.android.slowcarremotecontrol.tool.Constant;
import minuy.android.slowcarremotecontrol.tool.HanderHelper;

public class ViewService extends Thread {
    Context context;

    int cls,crs,cl,cr;
    int bcls,bcrs,bcl,bcr;
    boolean speedLock = false;
    HanderHelper handerHelper;

    StringBuilder stringBuilder = new StringBuilder();
    int stringBuilderSize = 0;

    @Override
    public void run() {

    }

    public ViewService(Context context){
        this.context = context;
        handerHelper = HanderHelper.getHanderHelper();
    }

    public void progressChanged(SeekBar seekBar, int progress){
        try {
            if (seekBar.getId() == R.id.vsbL || seekBar.getId() == R.id.vsbR) {
                if (speedLock) {
                    if (seekBar.getId() == R.id.vsbR) {
                        int val = 30-progress;
                        setMSpeedView(Constant.LIFT, progress);
                        setMSpeedView(Constant.RIGHT, progress);
                        cl = val;
                        cr = val;
                        handerHelper.sendHanderMsg(Constant.LOCK_L);//同步左边

                        setMSpeed(Constant.LIFT, val);
                        Thread.sleep(16);
                        setMSpeed(Constant.RIGHT, val);
                    }
                } else {
                    if (seekBar.getId() == R.id.vsbL) {
                        //左速度

                        int val = 30-progress;
                        setMSpeedView(Constant.LIFT, progress);
                        cl = val;

                        setMSpeed(Constant.LIFT, val);
                    } else if (seekBar.getId() == R.id.vsbR) {
                        //右速度
                        int val =30-progress;
                        setMSpeedView(Constant.RIGHT, progress);
                        cr = val;

                        setMSpeed(Constant.RIGHT, val);
                    }
                }
            } else if (seekBar.getId() == R.id.vsbLS) {
                //左状态
                if (progress < 33) {
                    //倒退
                    cls = 2;
                    setMStaView(Constant.LIFT, cls);

                    setMSta(Constant.LIFT, cls);
                } else if (progress > 66) {
                    //前进
                    cls = 1;
                    setMStaView(Constant.LIFT, cls);

                    setMSta(Constant.LIFT, cls);
                } else {
                    //停止
                    cls = 0;
                    setMStaView(Constant.LIFT, cls);

                    setMSta(Constant.LIFT, cls);
                }
            } else if (seekBar.getId() == R.id.vsbRS) {
                //右状态
                if (progress < 33) {
                    //倒退
                    crs = 2;
                    setMStaView(Constant.RIGHT, crs);

                    setMSta(Constant.RIGHT, crs);
                } else if (progress > 66) {
                    //前进
                    crs = 1;
                    setMStaView(Constant.RIGHT, crs);

                    setMSta(Constant.RIGHT, crs);
                } else {
                    //停止
                    crs = 0;
                    setMStaView(Constant.RIGHT, crs);

                    setMSta(Constant.RIGHT, crs);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        UpCarStaView();
    }

    //更新总视图
    private void UpCarStaView() {
        if(cls == 1 && crs == 1)
        {
            if(cr == cl) {
                handerHelper.sendHanderMsg(Constant.STA,"直行");
            }else if(cr>cl){
                handerHelper.sendHanderMsg(Constant.STA,"左转");
            }else if(cr<cl){
                handerHelper.sendHanderMsg(Constant.STA,"右转");
            }
        }else if(cls == 0 && crs == 0){
            handerHelper.sendHanderMsg(Constant.STA,"停止");
        }else if(cls == 1 && crs == 2){
            if(cr!=0 && cr!=0) {
                handerHelper.sendHanderMsg(Constant.STA,"右转");
            }else{
                handerHelper.sendHanderMsg(Constant.STA,"停止");
            }
        }else if(cls == 2 && crs == 1){
            if(cr!=0 && cr!=0) {
                handerHelper.   sendHanderMsg(Constant.STA,"左转");
            }else{
                handerHelper.sendHanderMsg(Constant.STA,"停止");
            }
        }else if(cls == 0 && crs == 1){
            if(cr!=0){
                handerHelper.sendHanderMsg(Constant.STA,"左转");
            }else{
                handerHelper.sendHanderMsg(Constant.STA,"停止");
            }
        }else if(cls == 0 && crs == 2){
            if(cr!=0){
                handerHelper.sendHanderMsg(Constant.STA,"左倒车");
            }else{
                handerHelper.sendHanderMsg(Constant.STA,"停止");
            }
        }else if(cls == 1 && crs == 0){
            if(cl!=0){
                handerHelper.sendHanderMsg(Constant.STA,"右转");
            }else{
                handerHelper.sendHanderMsg(Constant.STA,"停止");
            }
        }else if(cls == 2 && crs == 0){
            if(cl!=0){
                handerHelper.sendHanderMsg(Constant.STA,"右倒车");
            }else{
                handerHelper.sendHanderMsg(Constant.STA,"停止");
            }
        }else if(cls == 2 && crs == 2){
            if(cr == cl) {
                handerHelper.sendHanderMsg(Constant.STA,"倒车");
            }else if(cr>cl){
                handerHelper.sendHanderMsg(Constant.STA,"左倒车");
            }else if(cr<cl){
                handerHelper.sendHanderMsg(Constant.STA,"右倒车");
            }
        }
    }

    //设置状态视图变更
    private void setMStaView(String ver,int sta)
    {
        String lab = "";
        String str = "";
        if(ver.equals(Constant.LIFT)){
            lab += "左电机";
            switch (sta){
                case 0:
                    str = "停止";
                    lab += str;
                    break;
                case 1:
                    str = "前进";
                    lab += str;
                    break;
                case 2:
                    str = "倒退";
                    lab += str;
                    break;
            }
            handerHelper.sendHanderMsg(Constant.STA_L,str);//更新大左状态
            handerHelper.sendHanderMsg(Constant.STA_S_L,lab);//更新小左状态
        }else if(ver.equals(Constant.RIGHT)){
            lab += "右电机";
            switch (sta){
                case 0:
                    str = "停止";
                    lab += str;
                    break;
                case 1:
                    str = "前进";
                    lab += str;
                    break;
                case 2:
                    str = "倒退";
                    lab += str;
                    break;
            }
            handerHelper.sendHanderMsg(Constant.STA_R,str);//更新大右状态
            handerHelper.sendHanderMsg(Constant.STA_S_R,lab);//更新小右状态
        }
    }

    //设置速度视图变更
    private void setMSpeedView(String ver,int su){
        su++;
        String lab = "";
        if(ver.equals(Constant.LIFT)){
            lab += "左电机: ";
            if(su<10){
                lab +="0"+su;
            }else{
                lab +=su+"";
            }
            handerHelper.sendHanderMsg(Constant.SPEED_L,lab);//更新左电机速度显示
        }else if(ver.equals(Constant.RIGHT)) {
            lab += "右电机: ";
            if (su < 10) {
                lab += "0" + su;
            } else {
                lab += su + "";
            }
            handerHelper.sendHanderMsg(Constant.SPEED_R,lab);//更新右电机速度显示
        }
    }

    //发送状态指令
    private void setMSta(String ver,int sta){
        //检查是否更改
        if(ver.equals(Constant.LIFT)){
            if(sta == bcls)
            {
                return;
            }else {
                bcls = sta;
            }
        }else if(ver.equals(Constant.RIGHT)){
            if(sta == bcrs)
            {
                return;
            }else {
                bcrs = sta;
            }
        }

        if(!(sta>=0&&sta<=2)){
            return;
        }
        String cmd = "C"+ver+"S"+sta+"\r\n";

        //通过蓝牙传送cmd
        BluetoothSendStr(cmd);
    }

    //发送速度改变指令
    private void setMSpeed(String ver,int su){
        //检查是否更改
        if(ver.equals(Constant.LIFT)){
            if(su == bcl)
            {
                return;
            }else {
                bcl = su;
            }
        }else if(ver.equals(Constant.RIGHT)){
            if(su == bcr)
            {
                return;
            }else {
                bcr = su;
            }
        }

        if(su>30){
            su = 30;
        }else if(su<1){
            su = 1;
        }
        String cmd = "C"+ver;
        if(su<10){
            cmd+="0"+su;
        }else{
            cmd+=su+"";
        }
        cmd += "\r\n";
        //通过蓝牙传送cmd
        BluetoothSendStr(cmd);
    }

    private void BluetoothSendStr (String cmd){

        handerHelper.sendHanderMsg(Constant.BLUE_SEND,cmd);//发送到蓝牙

        stringBuilderSize++;
        if (stringBuilderSize > 27) {
            stringBuilderSize = 0;
            stringBuilder = new StringBuilder();
        }
        stringBuilder.append(cmd.replaceAll("\r\n", " | "));

        handerHelper.sendHanderMsg(Constant.EDIT, stringBuilder.toString());
    }

    public void setLock(boolean isc){
        this.speedLock = isc;
    }
}
